package com.whq.thrift.api.web.admin.controller.bs.requestlog;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.whq.common.StringUtil;
import com.whq.thrift.api.web.admin.db.requestlog.StatRequestHourApiData;
import com.whq.thrift.api.web.admin.service.requestlog.StatRequestHourApiDataService;
import com.whq.thrift.api.web.admin.util.DateFormatUtil;
import com.whq.thrift.api.web.admin.util.PageData;

@Controller
@Scope("prototype")
public class StatRequestHourApiDataController {
	@Autowired
	private StatRequestHourApiDataService statRequestHourApiDataService;

	private static final int DEFAULT_PAGE_SIZE = 25;

	@RequestMapping(value = "bs/requestlog/statrequesthourapidata/list")
	public String list(@RequestParam(value = "dataDate", required = false) String dataDate, 
			@RequestParam(value = "api", required = false) String api,
			@RequestParam(value = "dataHour", required = false) String dataHour,
			@RequestParam(value = "errorCode", required = false) String errorCode,
			@RequestParam(value = "ip", required = false) String ip,
			@RequestParam(value = "currentPage", required = true, defaultValue = "1") Integer currentPage,
			@RequestParam(value = "pageSize", required = false) Integer pageSize, Model model) {

		PageData<StatRequestHourApiData> pageData = new PageData<StatRequestHourApiData>();
		pageData.setCurrentPage(currentPage);
		if ((pageSize == null) || (pageSize <= 0)) {
			pageSize = DEFAULT_PAGE_SIZE;
		}
		pageData.setPageSize(pageSize);

		// 查询参数
		Map<String, Object> whereParams = new HashMap<String, Object>(0);
		if(StringUtil.isNotBlank(dataDate)){
			whereParams.put("dataDate", DateFormatUtil.dateToString(DateFormatUtil.formatToDate(dataDate)));
		}
		
		if(StringUtil.isNotBlank(api)){
			whereParams.put("api", api);
		}
		
		if(StringUtil.isNotBlank(dataHour)){
			whereParams.put("dataHour", dataHour);
		}
		
		if(StringUtil.isNotBlank(errorCode)){
			whereParams.put("errorCode", errorCode);
		}
		
		if(StringUtil.isNotBlank(ip)){
			whereParams.put("ip", ip);
		}
		
		int totalCount =this.statRequestHourApiDataService.findRecrodCount(whereParams);
		pageData.setTotalCount(totalCount);

		int beginIndex = (currentPage - 1) * pageSize;
		List<StatRequestHourApiData> resultList =this.statRequestHourApiDataService.findRecrods(whereParams, beginIndex, pageSize);
		pageData.setResultList(resultList);

		model.addAttribute("dataDate", dataDate);
		model.addAttribute("errorCode", errorCode);
		model.addAttribute("dataHour", dataHour);
		model.addAttribute("api", api);
		model.addAttribute("ip", ip);
		model.addAttribute("pageData", pageData);
		return "bs/requestlog/statrequesthourapidata/list";
	}
}
